import { Injectable } from "@nestjs/common";
import { TalentStoneService } from "../services/talent-stone.service";

@Injectable()
export class TalentStoneWsHandlers {
  constructor(
    private readonly talentStoneService: TalentStoneService
  ) { }

  /**
   * 处理天赋石相关指令
   */
  async handleTalentStoneCommand(command: number, data: any, characterId: number) {
    switch (command) {
      case 6035:
        return await this.handleTalentStoneList(data, characterId);
      case 6184:
        return await this.handleTalentStoneDetail(data, characterId);
      case 6186:
        return await this.handleTalentStoneAction(data, characterId);
      case 6185:
        return await this.handleAbsorbTalentStone(data, characterId);
      case 6189:
        return await this.handleGetAbsorbableStones(data, characterId);
      case 6188:
        return await this.handleGetEquippedStones(data, characterId);
      case 6187:
        return await this.handleDeputyTalentStoneAction(data, characterId);
      default:
        return {
          act_id: command,
          code: -1,
          msg: "未知的天赋石指令"
        };
    }
  }

  /**
   * 获取天赋石列表 (6035)
   */
  private async handleTalentStoneList(data: any, characterId: number) {
    const { pageNum = 0 } = data;
    return await this.talentStoneService.getTalentStoneList(characterId, pageNum);
  }

  /**
   * 查看天赋石详情 (6184)
   */
  private async handleTalentStoneDetail(data: any, characterId: number) {
    const { id } = data;
    if (!id) {
      return {
        act_id: 6184,
        code: -1,
        msg: "缺少天赋石ID"
      };
    }
    return await this.talentStoneService.getTalentStoneDetail(id, characterId);
  }

  /**
   * 天赋石操作 (6186)
   * act: 1=卸下, 2=装备到角色, 3=激活, 5=升级
   */
  private async handleTalentStoneAction(data: any, characterId: number) {
    const { id, act } = data;
    if (!id || !act) {
      return {
        act_id: 6186,
        code: -1,
        msg: "缺少必要参数"
      };
    }

    switch (act) {
      case "1":
        return await this.talentStoneService.unequipTalentStone(id, characterId);
      case "2":
        return await this.talentStoneService.equipTalentStoneToCharacter(id, characterId);
      case 3:
        return await this.talentStoneService.activateTalentStone(id, characterId);
      case "5":
        return await this.talentStoneService.upgradeTalentStone(id, characterId);
      default:
        return {
          act_id: 6186,
          code: -1,
          msg: "无效的操作类型"
        };
    }
  }

  /**
   * 吸收天赋石 (6185)
   */
  private async handleAbsorbTalentStone(data: any, characterId: number) {
    const { id, xid } = data;
    if (!id || !xid) {
      return {
        act_id: 6185,
        code: -1,
        msg: "缺少必要参数"
      };
    }
    return await this.talentStoneService.absorbTalentStone(id, xid, characterId);
  }

  /**
   * 获取可吸收的天赋石列表 (6189)
   */
  private async handleGetAbsorbableStones(data: any, characterId: number) {
    const { id, pageNum = 0 } = data;
    if (!id) {
      return {
        act_id: 6189,
        code: -1,
        msg: "缺少目标天赋石ID"
      };
    }
    return await this.talentStoneService.getAbsorbableTalentStones(id, characterId, pageNum);
  }

  /**
   * 获取已装备的天赋石列表 (6188)
   */
  private async handleGetEquippedStones(data: any, characterId: number) {
    const { cid = 0, pageNum = 0 } = data;
    return await this.talentStoneService.getEquippedTalentStones(characterId, cid, pageNum);
  }

  /**
   * 副将天赋石操作 (6187)
   */
  private async handleDeputyTalentStoneAction(data: any, characterId: number) {
    const { cid, sid, act } = data;
    if (!cid || !sid || !act) {
      return {
        act_id: 6187,
        code: -1,
        msg: "缺少必要参数"
      };
    }
    return await this.talentStoneService.equipTalentStoneToDeputy(sid, cid, characterId, act);
  }
}
